package com.xlh.service.course.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xlh.dao.course.TemplateMapper;
import com.xlh.dao.course.ext.TemplateMapperExt;
import com.xlh.dto.course.TemplateDTO;
import com.xlh.dto.course.TemplateDataDTO;
import com.xlh.dto.course.TemplateDetailDTO;
import com.xlh.dto.course.TemplateInfoDTO;
import com.xlh.dto.user.UserInfoDTO;
import com.xlh.enums.FileTypeEnum;
import com.xlh.enums.LocalResourcesEnum;
import com.xlh.enums.ResourcesTypeEnum;
import com.xlh.enums.course.TemplateTypeEnum;
import com.xlh.enums.user.IdentityEnum;
import com.xlh.exception.DownloadFileException;
import com.xlh.exception.common.BadRequestException;
import com.xlh.exception.common.GlobalException;
import com.xlh.exception.common.NotFoundException;
import com.xlh.param.course.TemplateInfoParam;
import com.xlh.pojo.course.Template;
import com.xlh.pojo.course.TemplateExample;
import com.xlh.pojo.system.Resource;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.MdHtmlFileService;
import com.xlh.service.course.TemplateService;
import com.xlh.service.system.SystemResourceService;
import com.xlh.service.user.UserService;
import com.xlh.util.BeanUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 模版
 * <p>
 * Created by lx on 2019/3/10 0010.
 */
@Service
public class TemplateServiceImpl implements TemplateService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TemplateServiceImpl.class);

    @Autowired
    private UserService userService;

    @Autowired
    private TemplateMapperExt templateMapperExt;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private TemplateMapper templateMapper;

    @Autowired
    private SystemResourceService resourceService;

    @Autowired
    private MdHtmlFileService mdHtmlFileService;

    @Override
    public Map<String, TemplateDTO> queryTemplateInfo(Long userId, TemplateTypeEnum typeEnum) {

        // 获取用户显示的内容
        Map<String, TemplateDTO> templateMap = queryTemplate(userId, typeEnum,
                Lists.newArrayList(ResourcesTypeEnum.TEMPLATE_WORD, ResourcesTypeEnum.TEMPLATE_ONLINE));

        // markdown内容读取转换
        return convertTemplateDTO(templateMap);
    }

    @Override
    public List<TemplateInfoDTO> queryTemplates(Long userId, TemplateTypeEnum typeEnum) {

        // 获取教师自己的模板
        List<TemplateDetailDTO> templateInfo = getTemplateInfo(
                Lists.newArrayList(userId), typeEnum.getCode(),
                Lists.newArrayList(ResourcesTypeEnum.EVALUATION.getCode()));

        // 获取预置的模板
        if (CollectionUtils.isEmpty(templateInfo)) {
            templateInfo = getTemplateInfo(null, typeEnum.getCode(),
                    Lists.newArrayList(ResourcesTypeEnum.EVALUATION.getCode()));
        }
        return convertTemplateInfoDTO(templateInfo);
    }

    @Override
    @Transactional
    public void updateTemplateInfo(Long userId, TemplateTypeEnum typeEnum, List<TemplateInfoParam> params) {

        List<TemplateDetailDTO> infos = getTemplateInfo(
                Lists.newArrayList(userId), typeEnum.getCode(),
                Lists.newArrayList(ResourcesTypeEnum.EVALUATION.getCode()));
        List<String> files = Lists.newArrayList();
        if (CollectionUtils.isEmpty(infos)) {

            // 设置文本名称
            for (int i = 0; i < 4; i++) {
                files.add(UUID.randomUUID().toString());
            }

            // 添加
            List<Resource> resources = convertResource(ResourcesTypeEnum.EVALUATION, params, files);
            resourceService.insertBatch(resources);

            List<Template> templates = convertTemplate(
                    resources.stream().map(Resource::getId).collect(Collectors.toList()), userId, typeEnum);
            if (CollectionUtils.isNotEmpty(templates)) {
                templateMapperExt.insertBatch(templates);
            }
        } else {
            // 修改
            List<Resource> resources = Lists.newArrayList();

            for (int i = 0; i < infos.size(); i++) {
                TemplateDetailDTO info = infos.get(i);

                Resource resource = new Resource();
                resource.setId(info.getResourceId());
                resource.setName(params.get(i).getName());
                resource.setRemark(params.get(i).isSelected() ? "1" : "0");
                resources.add(resource);
                files.add(info.getFileName());
            }

            resourceService.updateBatch(resources);
        }

        // 保存文本
        for (int i = 0; i < params.size(); i++) {
            String s = files.get(i);
            FileUtil.del(new File(LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath(s)));
            mdHtmlFileService.writeFile(params.get(i).getContent(),
                    LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath(), s);
        }

    }

    @Override
    public TemplateDataDTO queryTemplate(Long userId, TemplateTypeEnum typeEnum) {

        TemplateDataDTO result = new TemplateDataDTO();

        // 获取模版
        Map<String, TemplateDTO> templateDTOMap = queryTemplateInfo(userId, typeEnum);

        // 转换数据
        Lists.newArrayList(ResourcesTypeEnum.TEMPLATE_WORD, ResourcesTypeEnum.TEMPLATE_ONLINE).forEach(keyEnum -> {
            String key = getKey(keyEnum);
            TemplateDTO templateDTO = templateDTOMap.get(key);

            if (templateDTO != null && templateDTO.getData() != null) {
                if (keyEnum == ResourcesTypeEnum.TEMPLATE_WORD) {
                    result.setFilePath(templateDTO.getData().getFilePath());
                }
                if (keyEnum == ResourcesTypeEnum.TEMPLATE_ONLINE) {
                    result.setEditContent(templateDTO.getData().getEditContent());
                }
            }
        });

        return result;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOrUpdateTemplate(MultipartFile file, String editorContent,
                                       TemplateTypeEnum typeEnum, ResourcesTypeEnum resourcesTypeEnum) {

        // 检查参数
        checkCreateOrUpdateTemplateParam(typeEnum, resourcesTypeEnum, file, editorContent);

        // 获取用户显示的内容
        Map<String, TemplateDTO> templateMap = queryTemplate(userInfoHolder.getUserId(), typeEnum,
                Lists.newArrayList(resourcesTypeEnum));

        // 获取resource基本信息
        String name = resourcesTypeEnum == ResourcesTypeEnum.TEMPLATE_WORD ? file.getOriginalFilename() : "";
        String fileName = RandomUtil.simpleUUID();

        fileName = resourcesTypeEnum == ResourcesTypeEnum.TEMPLATE_WORD ?
                StringUtils.join(fileName, ".", StrUtil.subAfter(file.getOriginalFilename(), ".", true)) :
                StringUtils.join(fileName, FileTypeEnum.MARKDOWN.getExtension());

        // 如果有数据的情况下，角色是管理员 或者 教师自己创作的 。对resource表进行修改
        if (MapUtils.isNotEmpty(templateMap)
                && CollectionUtils.isNotEmpty(templateMap.values())
                && Lists.newArrayList(templateMap.values()).get(0).getData() != null
                && (userInfoHolder.isAdmin() || Lists.newArrayList(templateMap.values()).get(0).getOwnFlag())) {
            TemplateDetailDTO data = Lists.newArrayList(templateMap.values()).get(0).getData();
            updateResource(data.getResourceId(), fileName, name, "");

            // 删除原来的静态资源
            if (StringUtils.isNotBlank(data.getFileName())) {
                String filePath = typeEnum == TemplateTypeEnum.COURSE_REPORT ?
                        LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath(data.getFileName()) :
                        LocalResourcesEnum.TEST_TEMPLATE.getLocalUploadPath(data.getFileName());
                FileUtil.del(new File(filePath));
            }
        } else {
            // 如果当无数据 或者 非教师自己创作的。对resource、模板表进行插入
            Long resourceId = insertResource(fileName, name, "", resourcesTypeEnum.getCode());
            insertTemplate(userInfoHolder.isAdmin() ? null : userInfoHolder.getUserId(), typeEnum.getCode(), resourceId);
        }

        // 下载markdown
        if (resourcesTypeEnum == ResourcesTypeEnum.TEMPLATE_ONLINE) {
            String localUploadPath = typeEnum == TemplateTypeEnum.COURSE_REPORT ?
                    LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath() :
                    LocalResourcesEnum.TEST_TEMPLATE.getLocalUploadPath();
            mdHtmlFileService.writeFile(editorContent, localUploadPath, fileName);
        }

        // 下载文件
        if (resourcesTypeEnum == ResourcesTypeEnum.TEMPLATE_WORD) {
            String filePath = typeEnum == TemplateTypeEnum.COURSE_REPORT ?
                    LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath() :
                    LocalResourcesEnum.TEST_TEMPLATE.getLocalUploadPath();
            try {
                FileUtil.mkdir(filePath);
                file.transferTo(new File(StringUtils.join(filePath, File.separator, fileName)));
            } catch (IOException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTemplateResources(TemplateTypeEnum typeEnum) {

        // 获取用户显示的内容
        Map<String, TemplateDTO> templateMap = queryTemplate(userInfoHolder.getUserId(), typeEnum,
                Lists.newArrayList(ResourcesTypeEnum.TEMPLATE_WORD));

        // 如果是管理员 或者 是教师本人创作的。就修改resource表
        if (userInfoHolder.isAdmin()
                || (userInfoHolder.isTeacher()
                && MapUtils.isNotEmpty(templateMap) && CollectionUtils.isNotEmpty(templateMap.values())
                && Lists.newArrayList(templateMap.values()).get(0).getOwnFlag())) {
            TemplateDetailDTO templateDetailDTO = Lists.newArrayList(templateMap.values()).get(0).getData();
            updateResource(templateDetailDTO.getResourceId(), "", "", "");

            // 删除静态资源
            if (StringUtils.isNotBlank(templateDetailDTO.getFileName())) {
                String filePath = typeEnum == TemplateTypeEnum.COURSE_REPORT ?
                        LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath(templateDetailDTO.getFileName()) :
                        LocalResourcesEnum.TEST_TEMPLATE.getLocalUploadPath(templateDetailDTO.getFileName());
                FileUtil.del(new File(filePath));
            }

        } else {
            // 只插入模板表、资源表
            Long id = insertResource("", "", "", ResourcesTypeEnum.TEMPLATE_WORD.getCode());
            insertTemplate(userInfoHolder.getUserId(), typeEnum.getCode(), id);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resetCommonTemplate(TemplateTypeEnum templateTypeEnum, ResourcesTypeEnum resourcesTypeEnum) {

        List<TemplateDetailDTO> templateInfo = getTemplateInfo(
                Lists.newArrayList(userInfoHolder.getUserId()), templateTypeEnum.getCode(),
                Lists.newArrayList(resourcesTypeEnum.getCode()));
        if (CollectionUtils.isEmpty(templateInfo)) {
            throw new GlobalException("当前模板为通用模板，暂无法恢复");
        }

        TemplateExample example = new TemplateExample();
        example.createCriteria().andIdIn(
                templateInfo.stream().map(TemplateDetailDTO::getId).collect(Collectors.toList()));
        templateMapper.deleteByExample(example);
        resourceService.deleteBatch(
                templateInfo.stream().map(TemplateDetailDTO::getResourceId).collect(Collectors.toList()));

        // 删除静态资源
        templateInfo.forEach(templateDetailDTO -> {
            if (StringUtils.isNotBlank(templateDetailDTO.getFileName())) {
                String filePath = templateTypeEnum == TemplateTypeEnum.COURSE_REPORT ?
                        LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath(templateDetailDTO.getFileName()) :
                        LocalResourcesEnum.TEST_TEMPLATE.getLocalUploadPath(templateDetailDTO.getFileName());
                FileUtil.del(new File(filePath));
            }
        });

    }

    @Override
    public List<String> deleteBatchTemplate(List<Long> userIds) {

        List<TemplateDetailDTO> templateInfo = getTemplateInfo(userIds, null, null);
        if (CollectionUtils.isEmpty(templateInfo)) return null;

        List<Long> templateIds = templateInfo.stream().map(TemplateDetailDTO::getId).collect(Collectors.toList());

        TemplateExample example = new TemplateExample();
        example.createCriteria().andIdIn(templateIds);
        templateMapper.deleteByExample(example);

        // 删除resourceIds
        resourceService.deleteBatch(
                templateInfo.stream().map(TemplateDetailDTO::getResourceId).collect(Collectors.toList()));

        return templateInfo.stream()
                .filter(templateDetailDTO -> StringUtils.isNotBlank(templateDetailDTO.getFileName()))
                .map(templateDetailDTO ->
                        TemplateTypeEnum.getByCode(templateDetailDTO.getType()) == TemplateTypeEnum.COURSE_REPORT ?
                                LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath(templateDetailDTO.getFileName()) :
                                LocalResourcesEnum.TEST_TEMPLATE.getLocalUploadPath(templateDetailDTO.getFileName())
                ).collect(Collectors.toList());
    }

    @Override
    public String checkResource(Integer type, String fileName) {
        TemplateTypeEnum typeEnum = TemplateTypeEnum.getByCode(type);

        LocalResourcesEnum resourcesEnum = LocalResourcesEnum.COURSE_TEMPLATE;
        if (typeEnum == TemplateTypeEnum.TEST_REPORT) {
            resourcesEnum = LocalResourcesEnum.TEST_TEMPLATE;
        }

        if (!cn.hutool.core.io.FileUtil.exist(
                resourcesEnum.getLocalUploadPath(fileName))) {
            throw new DownloadFileException("暂无资源");
        }
        return resourcesEnum.getLocalUploadPath(fileName);
    }

    @Override
    public List<TemplateDetailDTO> getTemplateInfo(List<Long> userIds, Integer type, List<Integer> resourceTypes) {
        return templateMapperExt.getTemplateInfo(userIds, type, resourceTypes);
    }

    /**
     * 插入模板
     *
     * @param userId     用户id
     * @param type       模板类型
     * @param resourceId 资源id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertTemplate(Long userId, Integer type, Long resourceId) {
        Template record = new Template();
        record.setUserId(userId);
        record.setType(type);
        record.setResourceId(resourceId);
        templateMapper.insertSelective(record);
    }

    /**
     * 添加资源
     *
     * @param fileName 本地磁盘文件名
     * @param name     系统中文件名
     * @param remark   备注
     * @param type     资源类型
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertResource(String fileName, String name, String remark, Integer type) {
        Resource resource = new Resource();
        resource.setFileName(fileName);
        resource.setName(name);
        resource.setRemark(remark);
        resource.setType(type);
        resourceService.insertResource(resource);
        return resource.getId();
    }

    /**
     * 转换结果集
     *
     * @param templateInfo       用户模板信息
     * @param commonTemplates    通用模板信息
     * @param identityEnum       身份
     * @param resourcesTypeEnums 资源类型
     * @return
     */
    private Map<String, TemplateDTO> convertTemplateDetailDTO(List<TemplateDetailDTO> templateInfo,
                                                              List<TemplateDetailDTO> commonTemplates,
                                                              IdentityEnum identityEnum,
                                                              List<ResourcesTypeEnum> resourcesTypeEnums) {
        Map<String, TemplateDTO> resultMap = Maps.newHashMap();

        // 将两个参数的资源类型进行提取
        Map<Integer, TemplateDetailDTO> typeMap = BeanUtil.mapByKey("resourceType", templateInfo);
        Map<Integer, TemplateDetailDTO> commonTypeMap = BeanUtil.mapByKey("resourceType", commonTemplates);

        // 将每个结果集附上资源类型 及 当前资源类型下的结果数据
        resourcesTypeEnums.forEach(resourcesTypeEnum -> {

            // 将资源类型的名称 作为结果集的key
            String key = getKey(resourcesTypeEnum);

            // 根据资源类型获取map集合的结果集
            TemplateDetailDTO templateDetailDTOS = typeMap.get(resourcesTypeEnum.getCode());
            TemplateDetailDTO commonTemplateData = commonTypeMap.get(resourcesTypeEnum.getCode());

            // 如果身份是管理员 或者 身份是教师但当前教师没有数据，则将通用数据进行赋值；否则，将当前人员查出来的数据进行赋值
            if (identityEnum == IdentityEnum.ADMIN
                    || (identityEnum == IdentityEnum.TEACHER
                    && templateDetailDTOS == null)) {
                resultMap.put(key, new TemplateDTO(false, commonTemplateData));
            } else {
                resultMap.put(key, new TemplateDTO(true, templateDetailDTOS));
            }

        });

        return resultMap;
    }

    /**
     * 对markdown内容读取进行转换
     *
     * @param templateMap 结果集
     * @return
     */
    private Map<String, TemplateDTO> convertTemplateDTO(Map<String, TemplateDTO> templateMap) {

        if (MapUtils.isEmpty(templateMap)) return templateMap;

        // 获取在线编辑器的key
        String key = getKey(ResourcesTypeEnum.TEMPLATE_ONLINE);

        // 根据key获取value
        TemplateDTO templateDTO = templateMap.get(key);
        if (templateDTO == null) return templateMap;

        // 将数据获取到，为后续做准备
        TemplateDetailDTO templateDetailDTO = templateDTO.getData();
        if (templateDetailDTO == null) return templateMap;

        // 将md文件进行读取
        String localUploadPath = TemplateTypeEnum.COURSE_REPORT == TemplateTypeEnum.getByCode(templateDetailDTO.getType()) ?
                LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath() :
                LocalResourcesEnum.TEST_TEMPLATE.getLocalUploadPath();

        try {

            String editContent = mdHtmlFileService.readFile(localUploadPath, templateDetailDTO.getFileName());

            // 将内容赋值
            templateDTO.getData().setEditContent(editContent);

        } catch (Exception e) {
            throw new NotFoundException("资源不存在");
        }

        // 重新赋值给结果集
        templateMap.put(key, templateDTO);

        return templateMap;
    }

    /**
     * 检查添加或修改模板参数
     *
     * @param typeEnum          模板类型
     * @param resourcesTypeEnum 资源类型枚举
     * @param file              文件
     */
    private void checkCreateOrUpdateTemplateParam(TemplateTypeEnum typeEnum, ResourcesTypeEnum resourcesTypeEnum,
                                                  MultipartFile file, String editorContent) {
        if (resourcesTypeEnum == ResourcesTypeEnum.TEMPLATE_WORD && file == null) throw new BadRequestException();
        if (resourcesTypeEnum == ResourcesTypeEnum.TEMPLATE_WORD && StringUtils.isNotBlank(editorContent))
            throw new BadRequestException();
        if (resourcesTypeEnum == ResourcesTypeEnum.TEMPLATE_ONLINE && file != null) throw new BadRequestException();
        if (resourcesTypeEnum == ResourcesTypeEnum.TEMPLATE_ONLINE && editorContent == null)
            throw new BadRequestException();
        if (typeEnum == null || resourcesTypeEnum == null || (resourcesTypeEnum != ResourcesTypeEnum.TEMPLATE_WORD
                && resourcesTypeEnum != ResourcesTypeEnum.TEMPLATE_ONLINE)) {
            throw new BadRequestException();
        }
    }

    /**
     * 修改资源
     *
     * @param id       资源id
     * @param fileName 本地磁盘文件名
     * @param name     系统中文件名
     * @param remark   备注
     */
    private void updateResource(Long id, String fileName, String name, String remark) {
        Resource resource = new Resource();
        resource.setId(id);
        resource.setFileName(fileName);
        resource.setName(name);
        resource.setRemark(remark);
        resourceService.updateResource(resource);
    }

    /**
     * 获取模板
     *
     * @param userId            用户id
     * @param typeEnum          模板类型枚举
     * @param resourcesTypeEnum 资源类型枚举
     * @return
     */
    private Map<String, TemplateDTO> queryTemplate(Long userId, TemplateTypeEnum typeEnum,
                                                   List<ResourcesTypeEnum> resourcesTypeEnum) {

        if (userId == null || typeEnum == null || CollectionUtils.isEmpty(resourcesTypeEnum)) {
            throw new BadRequestException();
        }

        // 获取用户id的基本信息
        UserInfoDTO userInfoDTO = userService.getUserInfo(userId, null);

        // 获取当前人的模板
        List<TemplateDetailDTO> templateInfo = getTemplateInfo(Lists.newArrayList(userId),
                typeEnum.getCode(),
                resourcesTypeEnum.stream().map(ResourcesTypeEnum::getCode).collect(Collectors.toList()));

        // 根据基本信息获取身份
        IdentityEnum identityEnum = IdentityEnum.getByRole(userInfoDTO.getRole());

        // 获取管理员模板
        List<TemplateDetailDTO> commonTemplates = getTemplateInfo(null, typeEnum.getCode(),
                resourcesTypeEnum.stream().map(ResourcesTypeEnum::getCode).collect(Collectors.toList()));

        return convertTemplateDetailDTO(templateInfo, commonTemplates, identityEnum, resourcesTypeEnum);
    }

    private String getKey(ResourcesTypeEnum resourcesTypeEnum) {
        return Arrays.stream(StringUtils.split(resourcesTypeEnum.name(), "_"))
                .map(str -> StringUtils.join(str.substring(0, 1), str.substring(1).toLowerCase()))
                .reduce((str1, str2) -> StringUtils.join(str1, str2))
                .map(s -> StringUtils.join(s.substring(0, 1).toLowerCase(), s.substring(1))).get();
    }

    private List<TemplateInfoDTO> convertTemplateInfoDTO(List<TemplateDetailDTO> templateInfo) {

        List<TemplateInfoDTO> data = Lists.newArrayList();
        if (CollectionUtils.isEmpty(templateInfo)) return data;

        String localUploadPath = LocalResourcesEnum.COURSE_TEMPLATE.getLocalUploadPath();
        templateInfo.forEach(info -> {
            TemplateInfoDTO template = new TemplateInfoDTO();
            template.setId(info.getId());
            template.setName(info.getName());
            String content = "";
            try {
                content = mdHtmlFileService.readFile(localUploadPath, info.getFileName());
            }catch (NotFoundException e) {
            }
            template.setContent(content);
            template.setSelected(Objects.equals(info.getRemark(), "1") ? true : false);
            data.add(template);
        });
        return data;
    }

    private List<Template> convertTemplate(List<Long> ids, Long userId, TemplateTypeEnum typeEnum) {
        List<Template> list = Lists.newArrayList();
        ids.forEach(resourceId -> {
            Template template = new Template();
            template.setUserId(userId);
            template.setResourceId(resourceId);
            template.setType(typeEnum.getCode());
            list.add(template);
        });
        return list;
    }

    private List<Resource> convertResource(ResourcesTypeEnum typeEnum, List<TemplateInfoParam> params,
                                           List<String> files) {
        List<Resource> resources = Lists.newArrayList();
        for (int i = 0; i < params.size(); i++) {
            TemplateInfoParam param = params.get(i);
            Resource resource = new Resource();
            resource.setName(param.getName());
            resource.setType(typeEnum.getCode());
            resource.setFileName(files.get(i));
            resource.setRemark(param.isSelected() ? "1" : "0");
            resources.add(resource);
        }
        return resources;
    }

}
